import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:myflutterproject/routes.dart';
import 'package:myflutterproject/text.dart';
import 'package:myflutterproject/utils/overScrollBehavior.dart';
import 'package:myflutterproject/utils/toast.dart';
import 'package:myflutterproject/xiangqingpage.dart';

import 'navigatorpushnamed.dart';

class NavigatorDemo extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text("Navigator使用"),
        centerTitle: true,
      ),
      body: NavigatorPage(),
    );
  }
}

class NavigatorPage extends StatelessWidget {
  String _content =
      'Navigator 使用堆栈规则来管理 Widget，Navigator 记录了页面访问记录，可以使用 Navigator 来完成页面之间的跳转操作。在 Android 开发中，我们平时说的跳转都是指的是 Activity 的跳转，也称为页面跳转，在 Fluter 中都是指的是 Route 的跳转，Android 中的页面在 Flutter 中对应 Route，Navigator 负责 Route 对象堆栈的管理，并提供管理堆栈的方法，如 Navigator.push 和 Navigator.pop。';

  @override
  Widget build(BuildContext context) {
    return Center(
      child: ScrollConfiguration(
        behavior: OverScrollBehavior(),
        child: ListView(
          children: <Widget>[
            ItemButtonWithContent('Navigator简介', _content),
            TextWidget('入栈操作：', Colors.black),
            FlatButton(
              child: TextWidget('Navigator.push跳转(不传参)', Colors.blue),
              onPressed: () => {
                Navigator.push(context,
                    MaterialPageRoute(builder: (BuildContext context) {
                  return TextDemo();
                }))
              },
            ),
            FlatButton(
              child: TextWidget('Navigator.push跳转(传参)', Colors.blue),
              onPressed: () => {
                Navigator.push(context,
                    MaterialPageRoute(builder: (BuildContext context) {
                  return XQPageDemo('.push跳转(传参)', '参数内容');
                }))
              },
            ),
            FlatButton(
              child: TextWidget('Navigator.push跳转(传参,并带回信息)', Colors.blue),
              onPressed: () => {
                _navigatorToPageWithout2(context)
              },
              // onPressed: () => {_navigatorToPage3(context,NavigatorPushNamedPage.routeName,'This is Content')},
            ),
            FlatButton(
              child: TextWidget('Navigator.pushNamed跳转(不传参)', Colors.blue),
              onPressed: () => {Navigator.pushNamed(context, newText)},
            ),

          ],
        ),
      ),
    );
  }
}

class TextWidget extends StatelessWidget {
  final String name;
  final Color color;

  TextWidget(this.name, this.color);

  @override
  Widget build(BuildContext context) {
    return Container(
      padding: EdgeInsets.only(left: 10),
      child: Text(
        name,
        style: TextStyle(fontSize: 20, color: color),
      ),
    );
  }
}

/**
 * 按钮实现传参跳转
 */
class ItemButtonWithContent extends StatelessWidget {
  final String title;
  final String content;

  ItemButtonWithContent(this.title, this.content);

  @override
  Widget build(BuildContext context) {
    return FlatButton(
        onPressed: () => {
              _navigatorToPage2(context, title, content),
            },
        child: Text(
          title,
          style: TextStyle(fontSize: 20, color: Colors.blue),
        ));
  }
}

/**
 * push
 * 不带参数跳转
 * 通过push的方式跳转页面，同时传递数据到下一个页面，并接收处理后的结果(.then)
 */
_navigatorToPageWithout2(BuildContext context) {
  Navigator.push<String>(
    context,
    new MaterialPageRoute(builder: (BuildContext context) {
      return NavigatorPushNamedPage( 'Navigator.push跳转(传参,并带回信息)');
    }),
  ).then((result) async {
    print("===================上个页面带回信息$result");
  });
}

/**
 * push
 * 带参数跳转
 */
_navigatorToPage2(BuildContext context, String title, String content) {
  Navigator.push(
    context,
    MaterialPageRoute(builder: (BuildContext context) {
      return XQPageDemo(title, content);
    }),
  );
}


